<template>
  <div class="standard-table">

    <a-table
      bordered
      :loading="loading"
      :columns="columns"
      :dataSource="dataSource"
      :rowKey="rowKey"
      :pagination="pagination"
      :current='current'
      :pageSize='pageSize'
    >
      <template
        slot-scope="text, record, index"
        :slot="slot"
        v-for="slot in Object.keys($scopedSlots).filter(key => key !== 'expandedRowRender') "
      >
        <slot
          :name="slot"
          v-bind="{text, record, index}"
        ></slot>
      </template>
      <template
        :slot="slot"
        v-for="slot in Object.keys($slots)"
      >
        <slot :name="slot"></slot>
      </template>
      <template
        slot-scope="record, index, indent, expanded"
        :slot="$scopedSlots.expandedRowRender ? 'expandedRowRender' : ''"
      >
        <slot
          v-bind="{record, index, indent, expanded}"
          :name="$scopedSlots.expandedRowRender ? 'expandedRowRender' : ''"
        ></slot>
      </template>
    </a-table>
  </div>
</template>

<script>
export default {
  name: 'StandardTable',
  props: {
    loading: [Boolean, Object],
    columns: Array,
    dataSource: Array,
    rowKey: {
      type: [String, Function],
      default: 'key'
    },
    selectedRows: Array,
    expandedRowKeys: Array,
    expandedRowRender: Function
  },
  data() {
    return {
      needTotalList: [],
      pageSize: 10,
      current: 1,
      pagination: {
        showSizeChanger: true,
        showQuickJumper: false,
        showTotal: () => `共${this.dataSource.length}条`,
        total: this.dataSource.length,
      },
    }
  },
  methods: {
    initTotalList(columns) {
      const totalList = columns.filter(item => item.needTotal)
        .map(item => {
          return {
            ...item,
            total: 0
          }
        })
      return totalList
    },
  },
  created() {
    this.needTotalList = this.initTotalList(this.columns)
  },
}
</script>

<style scoped lang="less">
.standard-table {
  .alert {
    margin-bottom: 16px;
    .message {
      a {
        font-weight: 600;
      }
    }
    .clear {
      float: right;
    }
  }
}
</style>
